﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AddressBook.DAO;
using AddressBook.Entity;
using System.Text;

public partial class GetDept : System.Web.UI.Page
{
    static Base_DeptDAO base_deptDao = new Base_DeptDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.Name = "Dept";
        string where = string.Empty;
        if (!string.IsNullOrEmpty(Request.QueryString["phone"]))
        {
            where = " left join [Base_Employee] on LEFT(Base_Dept.DeptID,4) = LEFT(Base_Employee.DeptID,4) where Phone='" + Request.QueryString["phone"] + "' ";
            if (!string.IsNullOrEmpty(Request.QueryString["updatetime"]))
            {
                DateTime updatetime = DateTime.Parse("1900-01-01");
                DateTime.TryParse(Request.QueryString["updatetime"], out updatetime);
                where += " and Base_Dept.[UpdateTime] >= '" + updatetime.ToString("yyyy-MM-dd HH:mm:ss") + "'";
            }

            try
            {
                StringBuilder result = new StringBuilder();
                List<Base_Dept> lst = base_deptDao.GetInfos(" Base_Dept.* ", where, " order by PinYin ", string.Empty);

                foreach (Base_Dept dept in lst)
                {
                    result.Append("{");
                    result.AppendFormat("\"DeptID\":\"{0}\",", dept.DeptID);
                    result.AppendFormat("\"DeptName\":\"{0}\",", dept.DeptName.Replace("\"", "'"));
                    result.AppendFormat("\"DeptBriefName\":\"{0}\",", dept.DeptBriefName.Replace("\"", "'"));
                    result.AppendFormat("\"PinYin\":\"{0}\",", dept.PinYin);
                    result.AppendFormat("\"IsWorkDress\":\"{0}\",", dept.IsWorkDress);
                    result.AppendFormat("\"PhoneNum\":\"{0}\",", dept.PhoneNum);
                    result.AppendFormat("\"isDelete\":\"{0}\",", dept.isDelete);
                    result.AppendFormat("\"UpdateTime\":\"{0}\"", dept.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    result.Append("},");
                }
                if (lst.Count > 0)
                {
                    result.Remove(result.Length - 1, 1);
                }
                resultEntity.ResultCode = "0";
                resultEntity.Contents = result.ToString();
            }
            catch (Exception ex)
            {
                resultEntity.ResultCode = "-1";
                resultEntity.Desc = ex.Message.Replace("\"", "'");
            }
        }
        else
        {
            resultEntity.ResultCode = "-2";
            resultEntity.Desc = "参数不正确！";
        }
        Response.Write(Common.ReturnResult(resultEntity));
    }

    /*
     
     { "people": [ 　　{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, 　　{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"}, 　　{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } 　　]} 
     * 
     */
}